package service

import (
	"errors"
	"gorm.io/gorm"
	"server/client/db"
	"server/model"
	"time"
)

func SetBlacklist(jwtList model.Blacklist) (err error) {
	err = db.DB.Create(&jwtList).Error
	return
}

func IsInBlacklist(jwt string) bool {
	isNotFound := errors.Is(db.DB.Where("jwt = ?", jwt).First(&model.Blacklist{}).Error, gorm.ErrRecordNotFound)
	return !isNotFound
}

func GetRedisJWT(userName string) (err error, redisJWT string) {
	redisJWT, err = db.REDIS.Get(userName).Result()
	return err, redisJWT
}

func SetRedisJWT(jwt string, userName string) (err error) {
	// 此处过期时间等于jwt过期时间
	timer := 60 * 60 * 24 * 7 * time.Second
	err = db.REDIS.Set(userName, jwt, timer).Err()
	return err
}
